package blog.controller;

import blog.entity.Article;
import blog.entity.Comment;
import blog.entity.User;
import blog.service.ArticleService;
import blog.service.CommentService;
import blog.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller @RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private ArticleService articleService;
    @Resource
    private CommentService commentService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String gotoLogin(){
        return "login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String userName, String userPass, ModelMap map, HttpSession session){
         User user =  userService.login(userName, userPass);
         if(user == null){
             user = (User) session.getAttribute("session_user");
         }
         if(user!=null){
             session.setAttribute("session_user",user);
             List<Article> articleList = articleService.listRecentArticle(5);
             map.put("articleList",articleList);
             List<Comment> commentList = commentService.listRecentComment(5);
             map.put("commentList",commentList);
             return "index";
         }
         else {
             map.put("msg","用户名或密码错误");
             return "login";
         }
    }
    @RequestMapping("")
    public String userList(Map map){
        List<User> userList = userService.getAllUser();
        map.put("userList",userList);
        return "user/user-list";
    }
    @RequestMapping("/photo/{id}")
    public void showPhoto(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException {
        User user=userService.getUserById(id);
        response.setContentType("image/jpg");
        ServletOutputStream outStream=response.getOutputStream();
        outStream.write(user.getUserPhoto());
        outStream.flush();
    }
    @RequestMapping(value="/add",method= RequestMethod.GET)
    public String gotoAddPage() {
        return "user/user-add";
    }
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(User user, MultipartFile photo) throws IOException {
        user.setUserRegisterTime(new Date());
        user.setUserStatus(1);
        user.setUserPhoto(photo.getBytes());
        userService.addUser(user);
        return "forward:/user";   //添加完成以后.转到用望列表
    }
    @RequestMapping("/delete/{userId}")
    public String delete(@PathVariable("userId") Integer userId){
        userService.deleteUser(userId);
        return "forward:/user";
    }
    @RequestMapping(value = "/edit/{userId}",method = RequestMethod.GET)
    public String forEditUser(@PathVariable("userId") Integer userId,Map map){
        User u = userService.getUserById(userId);
        map.put("u",u);
        return "user/user-edit";
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String editUser(User user,MultipartFile photo) throws IOException {
        user.setUserPhoto(photo.getBytes());
        userService.updateUser(user);
        return "forward:/user";
    }
    @RequestMapping("/logout")
    public String logOut(HttpServletRequest request){
        request.getSession().setAttribute("session_user",null);
        return "forward:/user/login";
    }
}
